package com.duoketech.controller.weixin;

import com.duoketech.controller.BaseController;
import com.duoketech.exception.ValidateException;
import com.duoketech.model.receiver.Receiver;
import com.duoketech.service.weixin.ReceiverService;
import com.jfinal.plugin.activerecord.Page;

public class WxReceiverController extends BaseController {

    public void receiverList(){
        int memberId = getMemberId();
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",20);
        Page<Receiver> receiverPage = ReceiverService.ME.queryReceiverList(pageNum,pageSize,memberId);
        setAttr("receiverList",receiverPage);
        render("receiver-list.html");
    }

    public void ajaxReceiverList(){
        int memberId = getMemberId();
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",20);
        Page<Receiver> receiverPage = ReceiverService.ME.queryReceiverList(pageNum,pageSize,memberId);
        renderObject(receiverPage);
    }


    public void ajaxDelReceiver(){
        int id = getParaToInt("id",0);
        try {
            ReceiverService.ME.delReciver(id,getMemberId());
            renderSuccess("操作成功");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    public void ajaxMarkerReceiverAsDefault(){
        int id = getParaToInt("id",0);
        try {
            ReceiverService.ME.markerReceiverAsDefault(id,getMemberId());
            renderSuccess("操作成功");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    public void editReceiver(){
        int id = getParaToInt("id",0);
        setAttr("receiver",ReceiverService.ME.queryReceiverById(id));
        render("edit-receiver.html");
    }

    public void doUpdateReceiver(){
        try {
            Receiver receiver = getModel(Receiver.class,"");
            ReceiverService.ME.doUpdateReceiver(receiver,getMemberId());
            renderSuccess("操作成功");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    public void doAddReceiver(){
        try {
            Receiver receiver = getModel(Receiver.class,"");
            ReceiverService.ME.doAddReceiver(receiver,getMemberId());
            renderSuccess("操作成功");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    public void addReceiver(){
        int isReturn = getParaToInt("isReturn",0);
        setAttr("isReturn",isReturn);
        render("add-receiver.html");
    }

}
